<template>
  <div class="time-container">
    <div class="countdown-title">
      {{ !start ? '活动待开始' : '活动倒计时' }}
    </div>
    <div class="time-countdown">
      <div class="time-countdown-box" v-if="times && times.start && times.end">
        <BigdataCountdown
          v-if="!start"
          v-model="startTimestamp"
          format="DD天HH：mm ：ss : SSS"
          @finish="handleStartFinish"
          @change="handleChange"
        >
          <div slot="formatter">
            <div class="countdown-box">
              <span class="cube" v-if="dateMeta.showYear">{{ dateMeta.showYear }}</span>
              <span class="flag" v-if="dateMeta.showYear">年</span>
              <span class="cube" v-if="dateMeta.showMonth">{{ dateMeta.showMonth }}</span>
              <span class="flag" v-if="dateMeta.showMonth">月</span>
              <span class="cube" v-if="dateMeta.showDay">{{ dateMeta.showDay }}</span>
              <span class="flag" v-if="dateMeta.showDay">天</span>
              <span class="cube" v-if="dateMeta.showHour">{{ dateMeta.showHour }}</span>
              <span class="flag" v-if="dateMeta.showHour">:</span>
              <span class="cube" v-if="dateMeta.showMinute">{{ dateMeta.showMinute }}</span>
              <span class="flag" v-if="dateMeta.showMinute">:</span>
              <span class="cube" v-if="dateMeta.showSecond">{{ dateMeta.showSecond }}</span>
              <span class="flag" v-if="dateMeta.showSecond">:</span>
              <span class="cube"
                    v-if="dateMeta.showMillisecond">{{
                  String(dateMeta.showMillisecond || '--').substring(0, 1)
                }}</span>
            </div>
          </div>
        </BigdataCountdown>
        <BigdataCountdown
          v-if="start"
          v-model="endTimestamp"
          format="DD天HH：mm ：ss : SSS"
          @finish="handleFinish"
          @change="handleChange">
          <div slot="formatter">
            <div class="countdown-box">
              <span class="cube" v-if="dateMeta.showYear">{{ dateMeta.showYear }}</span>
              <span class="flag" v-if="dateMeta.showYear">年</span>
              <span class="cube" v-if="dateMeta.showMonth">{{ dateMeta.showMonth }}</span>
              <span class="flag" v-if="dateMeta.showMonth">月</span>
              <span class="cube" v-if="dateMeta.showDay">{{ dateMeta.showDay }}</span>
              <span class="flag" v-if="dateMeta.showDay">天</span>
              <span class="cube" v-if="dateMeta.showHour">{{ dateMeta.showHour }}</span>
              <span class="flag" v-if="dateMeta.showHour">:</span>
              <span class="cube" v-if="dateMeta.showMinute">{{ dateMeta.showMinute }}</span>
              <span class="flag" v-if="dateMeta.showMinute">:</span>
              <span class="cube" v-if="dateMeta.showSecond">{{ dateMeta.showSecond }}</span>
              <span class="flag" v-if="dateMeta.showSecond">:</span>
              <span class="cube"
                    v-if="dateMeta.showMillisecond">{{
                  String(dateMeta.showMillisecond || '--').substring(0, 1)
                }}</span>
            </div>
          </div>
          <div slot="finish">
            <div class="countdown-box">
              <span class="cube" >00</span>
              <span class="flag" >天</span>
              <span class="cube" >00</span>
              <span class="flag" >:</span>
              <span class="cube" >00</span>
              <span class="flag" >:</span>
              <span class="cube" >00</span>
              <span class="flag" >:</span>
              <span class="cube">00</span>
            </div>
          </div>
        </BigdataCountdown>
      </div>
      <div class="time-countdown-error" v-else>未配置</div>
    </div>
  </div>
</template>

<script>
import BigdataCountdown from "./BigdataCountdown";
import moment from "moment";
import {Message} from "element-ui";

export default {
  name: "TimeView",
  components: {
    BigdataCountdown,
  },
  props: {
    times: {
      type: Object,
      default: () => {
        return {
          start: null,
          end: null,
        }
      }
    }
  },
  computed: {
    startTimestamp() {
      if (this.times.start) {
        return moment(this.times.start).valueOf();
      }
      return moment().valueOf()
    },
    endTimestamp() {
      if (this.times.end) {
        return moment(this.times.end).valueOf();
      }
      return moment().valueOf()
    }
  },
  data() {
    return {
      start: false,
      dateMeta: {
        showMillisecond: 0,
        showYear: 0,
        showMonth: 0,
        showDay: 0,
        showHour: 0,
        showMinute: 0,
        showSecond: 0,
      }
    }
  },
  created() {
    this.initStart()
  },
  watch: {
    times: {
      handler(val) {
        if (val) {
          this.initStart();
        }
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    initStart() {
      if (this.times && this.times.start) {
        if (moment(this.times.start).valueOf() >= moment().valueOf()) {
          this.start = false
          return
        }
      }
      this.start = true
    },
    handleStartFinish() {
      this.start = true
    },
    handleFinish() {
      Message.warning('活动已结束')
    },
    handleChange(val) {
      if (val && val.meta) {
        this.dateMeta = val.meta
      }
    }
  }
}
</script>

<style scoped lang="scss">
.time-container {
  width: 100%;
  padding: 0;
  margin: 0;
  box-sizing: border-box;

  .countdown-title {
    color: #55c7ff;
    font-size: 1.5rem;
    font-weight: 700;
    text-align: center;
    padding: 10px 0;
  }

  .time-countdown-title {
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 24px;
    font-size: 14px;
    font-weight: 700;
    padding: 10px 0 7px;
    color: #fff;
    &:before,&:after {
      box-sizing: border-box;

      display: block;
      width: 20px;
      height: 1px;
      content: "";
      background: hsla(0, 0%, 100%, .8);
      margin: 0 10px;
    }
  }
  .time-countdown {
    text-align: center;
    .time-countdown-box {
      .countdown-box {
        font-size: 14px;
        color: var(--banner_color, #fff);
        line-height: 18px;
        display: inline-flex;
        align-items: center;
      }
      span {
        display: inline-block;
        font-size: 12px;
        border-radius: 3px;
      }
      .cube {
        text-align: center;
        width: 3.65rem;
        height: 4.1rem;
        line-height: 4rem;
        border-radius: .5rem;
        color: #74efff;
        font-size: 1.5rem;
        font-weight: 700;
        background-repeat: no-repeat;
        background-position: top;
        background-image: url("../../../../../assets/images/bigdata/center_little_bg.png");
        background-size: contain;
        background-color: unset;
      }
      .flag {
        color: #55c7ff;
        font-size: 1.5rem;
        font-weight: 700;
        line-height: 18px;
        display: inline-flex;
        align-items: center;
      }
    }
  }
}
</style>
